package com.lkx.sms.tool;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.util.SparseArray;
import com.lkx.Constants;
import com.lkx.sms.data.SmsContent;
import com.lkx.sms.data.SmsSession;

/**
 * @author lklx
 *
 */
/*URI主要有：

content://sms/               所有短信
content://sms/inbox        收件箱
content://sms/sent          已发送
content://sms/draft         草稿
content://sms/outbox     发件箱
content://sms/failed       发送失败
content://sms/queued    待发送列表


sms主要结构：
_id => 短消息序号 如100
thread_id => 对话的序号 如100
address => 发件人地址，手机号.如+8613811810000
person => 发件人，返回一个数字就是联系人列表里的序号，陌生人为null
date => 日期  long型。如1256539465022
protocol => 协议 0 SMS_RPOTO, 1 MMS_PROTO
read => 是否阅读 0未读， 1已读
status => 状态 -1接收，0 complete, 64 pending, 128 failed
type => 类型 1是接收到的，2是已发出
body => 短消息内容
service_center => 短信服务中心号码编号。如+8613800755500
String[] projection = new String[]{"address", "body"};
Cursor cursor = getContentResolver().query(uri, projection, "where .." new String[]{"", ""}, "order by ..")*/
public class SmsResolver {

	public static List<SmsSession> getSmsSessionList(ContentResolver contentResolver){
		List<SmsSession> smsSessionList = new ArrayList<SmsSession>();
		SparseArray<SmsSession> smsSessions = new SparseArray<SmsSession>();
		Uri uri = Uri.parse(Constants.SMS_URI_ALL);
		String[] projection = {"thread_id","address","date","protocol","read","status","type","body"};
		Cursor cursor = contentResolver.query(uri, projection, null, null, "date desc");
		if (cursor.getCount() > 0) {
			while(cursor.moveToNext()) {
				int threadIdIndex = cursor.getColumnIndex("thread_id");
				int addressIndex = cursor.getColumnIndex("address");
				int dateIndex = cursor.getColumnIndex("date");
				int protocolIndex = cursor.getColumnIndex("protocol");
				int readIndex = cursor.getColumnIndex("read");
				int statusIndex = cursor.getColumnIndex("status");
				int typeIndex = cursor.getColumnIndex("type");
				int bodyIndex = cursor.getColumnIndex("body");

				int threadId = cursor.getInt(threadIdIndex);
				SmsSession sess = null;
				if(smsSessions.get(threadId)==null) {
					String address = cursor.getString(addressIndex);
					sess = new SmsSession(threadId,address);
					smsSessions.put(threadId, sess);
				}else {
					sess = smsSessions.get(threadId);
				}
				long date = cursor.getLong(dateIndex);
				int protocol = cursor.getInt(protocolIndex);
				int read = cursor.getInt(readIndex);
				int status = cursor.getInt(statusIndex);
				int type = cursor.getInt(typeIndex);
				String body = cursor.getString(bodyIndex);
				SmsContent smsContent = new SmsContent(date, protocol, read, status, type, body);
				sess.addSmsContent(smsContent);
				sess.setLastUpdate(date);
			}
			for(int i=0,size=smsSessions.size();i<size;i++) {
				smsSessionList.add(smsSessions.valueAt(i));
			}
			Collections.sort(smsSessionList);
		}
		return smsSessionList;
	}
}
